//
//  LEPassedStaffController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/10.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LEPassedStaffController.h"
#import "LEStaffCell.h"
#import "LEStaffModel.h"
#import "MakerTools.h"

#import "LEStaffDetialController.h"

static NSString *const LEStaffCellId = @"LEStaffCell";

@interface LEPassedStaffController ()
/**
 worktype 店内 1 配送 2
 */
@property(nonatomic,copy)NSString *worktype;

@end

@implementation LEPassedStaffController

- (void)setupUI
{
    [super setupUI];

    UISegmentedControl *segement = [[UISegmentedControl alloc] initWithItems:@[@"店内员工",@"派送员工"]];
    segement.frame = CGRectMake(-2, 5, LEScreenWidth+4, 30);
    [segement setSelectedSegmentIndex:0];
    [segement addTarget:self action:@selector(didClickSegmentedControlAction:)forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segement];
    segement.tintColor = LESystemBlueColor;
    
    self.view.backgroundColor = LEHaxColor(0xf0f0f0);
    [self resetTablewFrame:CGRectMake(0, 35, LEScreenWidth, LEScreenHeight -64-40-40-49)];
    self.tableView.rowHeight = 100;
    [self.tableView registerNib:[UINib nibWithNibName:@"LEStaffCell" bundle:nil] forCellReuseIdentifier:LEStaffCellId];
    [self initHeader];
    [self initFooter];
}

- (void)loadData
{
    self.worktype = @"1";
    self.page = 1;
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.page) forKey:@"curpage"];
    [dict setObject:@"10" forKey:@"page"];
    [dict setObject:@"1" forKey:@"relation_verify"];
    [dict setObject:self.worktype forKey:@"service_place"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:StaffListUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        NSString *totalPage = successCode[@"page_total"];
        weakSelf.totalPage = totalPage;
        if (error)
        {
            [self addNoDataViewWithTipText:error];
            [weakSelf.dataSource removeAllObjects];
        }else
        {
            [self hiddenNoDataView];

            NSArray *serviceList = datas[@"service_list"];
            NSArray *tempModel = [LEStaffModel mj_objectArrayWithKeyValuesArray:serviceList];

            if (weakSelf.page == 1) {
                [weakSelf.dataSource removeAllObjects];
                [weakSelf.dataSource addObjectsFromArray:tempModel];

            }else{
                [weakSelf.dataSource addObjectsFromArray:tempModel];
            }
            
        }
        [weakSelf.tableView reloadData];
        
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}

/**
 改变角色
 */
- (void)requstChangeRole:(LEStaffModel *)model
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:model.relation_id forKey:@"relation_id"];
    if ([model.service_place isEqualToString:@"1"])
    {
        [dict setObject:@"2" forKey:@"service_place"];
    }
    if ([model.service_place isEqualToString:@"2"])
    {
        [dict setObject:@"1" forKey:@"service_place"];
    }
    
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:StaffChangeRolelUrl params:dict success:^(NSDictionary *successCode) {
        
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:datas];
            
            [weakSelf requestRefreshData];
        }
        
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}

/**
  请求删除员工

 @param model <#model description#>
 */
- (void)requestDeleteStaff:(LEStaffModel *)model
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:model.relation_id forKey:@"relation_id"];

    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:StaffRemoveStafflUrl params:dict success:^(NSDictionary *successCode) {
        
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:datas];
            
            [weakSelf requestRefreshData];
        }
        
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];

}

#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LEStaffCell *cell = [tableView dequeueReusableCellWithIdentifier:LEStaffCellId forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
     __weak typeof (self) weakSelf = self;
    [cell staffCellClick:^(NSInteger tag, LEStaffModel *model) {
        LELog(@"%s---%d---%ld",__func__,__LINE__,tag);
        if (tag == 2) {
            //转变角色
            [weakSelf requstChangeRole:model];
        }
        if (tag == 1) {
            //解除
            [weakSelf showAlertView:model];
        }
    }];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LEStaffDetialController *staffDetail = [[LEStaffDetialController alloc] init];
    LEStaffModel *model = self.dataSource[indexPath.row];
    staffDetail.store_id = model.store_id;
    staffDetail.service_id = model.service_id;
    [self.navigationController pushViewController:staffDetail animated:YES];
}
#pragma mark- 点击事件
- (void)didClickSegmentedControlAction:(UISegmentedControl *)segmentControl
{
    self.worktype = segmentControl.selectedSegmentIndex == 1 ? @"2" : @"1";
    self.page = 1;
    [self requestRefreshData];
}
#pragma mark- privateMethod
- (void)showAlertView:(LEStaffModel *)model
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定要解雇该员工" message: nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self requestDeleteStaff:model];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:sureAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}

@end












